package stock.service;

import com.mysql.jdbc.StringUtils;
import jdk.nashorn.internal.objects.NativeString;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.index.Terms;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import stock.bean.Goods;
import stock.repositories.ESGoodsRepository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ElasticsearchService {
    private final ESGoodsRepository esGoodsRepository;
    private final TransactionTemplate transactionTemplate;

    public ElasticsearchService(ESGoodsRepository esGoodsRepository,TransactionTemplate transactionTemplate){
        this.esGoodsRepository = esGoodsRepository;
        this.transactionTemplate = transactionTemplate;
    }

    public List<Goods> search(String keyword){
        return esGoodsRepository.find(keyword);
    }

    public List<Goods> searchHits(String keyword){
        List<Goods> res= esGoodsRepository.find(keyword);
        System.out.println(res);
        return res;
    }

    public List<Goods> findByTitle(String keyword){
        return esGoodsRepository.findByTitle(keyword);
    }
    public List<Goods> findByTitleOrPrice(Object keyword){
        List<Goods> res = esGoodsRepository.findByTitleOrPrice((String) keyword, 0.01);
        return res;
    }

    public List<Goods> findByTitleAndPrice(Object keyword){
        List<Goods> res = esGoodsRepository.findByTitleAndPrice((String) keyword, 0.01);
        return res;
    }
    public List<Goods> findByTitleOrCover(String keyword){
        List<Goods> res = esGoodsRepository.findByTitleOrCover(keyword, keyword);
        return res;
    }

//    public Object searchPage(String keyword,int page,int size){
//        Map<String,Object> map = new HashMap<>();
//        Pageable pageable = PageRequest.of(page,size);
//        System.out.println(keyword+page+size);
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        if(!StringUtils.isNullOrEmpty(keyword)){
//            boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("title",keyword));
//        }
//        FieldSortBuilder fieldSortBuilder = null;
//        fieldSortBuilder = SortBuilders.fieldSort("goods_id").order(SortOrder.DESC);
//
//        NativeSearchQuery queryBuilder = new NativeSearchQueryBuilder()
//                .withQuery(boolQueryBuilder)
//                .withSort(fieldSortBuilder)
//                .withPageable(pageable)
//                .build();
//        NativeString sdoIndexEnRepository;
//        AggregatedPage<Goods> search = (AggregatedPage)sdoIndexEnRepository.search(queryBuilder);
//        long totalElements = search.getTotalElements();
//        int totalPages = search.getTotalPages();
//        List<Goods> content = search.getContent();
//        Terms term1 = (Terms)search.getAggregations().getAsMap().get("taxonomy");
//        log.debug("term1============"+term1.toString());
//
//        System.out.println(queryBuilder);
//        return null;
//    }
}
